package com.itheima.controller;

import com.itheima.entity.Emp;
import com.itheima.entity.Result;
import com.itheima.service.EmpService;
import com.itheima.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class loginController {
    @Autowired
    EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        // 1.调用业务层，实现员工登录功能
        Emp empResult = empService.checkLogin(emp);
        // 2.封装返返回处理结果
        // 2.1 登录失败
        if (empResult == null) {
            return Result.error("登陆失败,please open your big eyes,check your info");
        }
        //2.2登录成功,颁发JWT令牌(用户身份标识：字符串)
        Map claims = new HashMap();
        claims.put("empId", empResult.getId());
        claims.put("username", empResult.getUsername());
        // 调用工具类，生成令牌
        String jwtTolenStr = JwtUtils.generateJwt(claims);
        //3.封装返回处理结果
        return Result.success(jwtTolenStr);
    }
}
